﻿@model Cxuu.Models.EditAdminUserGroup
@{
    ViewData["Title"] = "保存";
}
<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
                角色编辑
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/UserGroup/')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-form">
        <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <fieldset class="layui-elem-field">
                <legend><label asp-for="Groupname" class="layui-form-label"></label></legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                            <input type="text" asp-for="Groupname" value="" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">用户组名</div>
                    </div>
                </div>
            </fieldset>

            <fieldset class="layui-elem-field">
                <legend><label asp-for="Channlrole"></label></legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                            <div id="tree"></div>
                        </div>
                    </div>
                </div>
            </fieldset>

            <fieldset class="layui-elem-field">
                <legend><label asp-for="Systemrole" class="layui-form-label"></label></legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <label class="layui-form-label">后台管理</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="Home_Index" title="进入后台" checked disabled>
                            <input type="checkbox" name="Systemrole" value="Home_Home" title="后台首页" checked disabled>
                            <input type="checkbox" name="Systemrole" value="Log_Index" title="日志查看">
                            <input type="checkbox" name="Systemrole" value="Cache_Index" title="缓存查看">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">模块权限</label>
                        <div class="layui-input-block">                           
                            <input type="checkbox" name="Systemrole" value="Menu_Index" title="后台主页"  checked disabled>                           
                            <input type="checkbox" name="Systemrole" value="Menu_Content" title="内容管理">                           
                            <input type="checkbox" name="Systemrole" value="Menu_System" title="系统管理">
                            <input type="checkbox" name="Systemrole" value="Menu_Extend" title="扩展功能">                          
                        </div>

                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">系统设置</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="Config_Index" title="查看">
                            <input type="checkbox" name="Systemrole" value="Config_Edit" title="编辑">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">通知管理</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="Notice_Index" title="列表（接收）">
                            <input type="checkbox" name="Systemrole" value="Notice_AddOrEdit" title="发布(view)">
                            <input type="checkbox" name="Systemrole" value="Notice_CreatePost" title="发布">
                            <input type="checkbox" name="Systemrole" value="Notice_Delete" title="删除">
                            <input type="checkbox" name="Systemrole" value="Notice_Read" title="读取通知">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">上传管理</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="Upload_Upload" title="上传文件"> 
                            <input type="checkbox" name="Systemrole" value="Attment_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="Attment_AddOrEdit" title="添加(view)">
                            <input type="checkbox" name="Systemrole" value="Attment_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="Attment_Delete" title="删除">
                        </div>
                    </div>
              <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>后台用户管理</legend>
</fieldset>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色管理</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="UserGroup_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="UserGroup_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="UserGroup_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="UserGroup_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="UserGroup_Delete" title="删除">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户管理</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="User_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="User_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="User_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="User_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="User_Delete" title="删除">
                            <input type="checkbox" name="Systemrole" value="User_PassWord" title="自行修改密码">
                        </div>
                    </div>
                   <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>内容管理</legend>
</fieldset>
                    <div class="layui-form-item">
                        <label class="layui-form-label">栏目管理</label>
                        <div class="layui-input-block">                           
                            <input type="checkbox" name="Systemrole" value="Category_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="Category_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="Category_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="Category_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="Category_Delete" title="删除">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">内容管理</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="Systemrole" value="Category_GetTree" title="栏目树形列表">
                            <input type="checkbox" name="Systemrole" value="Content_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="Content_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="Content_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="Content_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="Content_Delete" title="删除">
                            <input type="checkbox" name="Systemrole" value="Content_Status" title="发布">
                            <input type="checkbox" name="Systemrole" value="Content_Att" title="显示属性">
                        </div>                        
                    </div>
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>扩展管理</legend>
</fieldset>
                    <div class="layui-form-item">
                        <label class="layui-form-label">成员管理</label>
                        <div class="layui-input-block">                           
                            <input type="checkbox" name="Systemrole" value="ExtMember_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="ExtMember_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="ExtMember_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="ExtMember_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="ExtMember_Delete" title="删除">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">视频列表</label>
                        <div class="layui-input-block">                           
                            <input type="checkbox" name="Systemrole" value="Extvideo_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="Extvideo_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="Extvideo_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="Extvideo_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="Extvideo_Delete" title="删除">
                            <input type="checkbox" name="Systemrole" value="Extvideo_Status" title="发布">
                            <input type="checkbox" name="Systemrole" value="Extvideo_Att" title="显示属性">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">视频栏目</label>
                        <div class="layui-input-block">                           
                            <input type="checkbox" name="Systemrole" value="ExtvideoCate_Index" title="查看列表">
                            <input type="checkbox" name="Systemrole" value="ExtvideoCate_AddOrEdit" title="添加/编辑(view)">
                            <input type="checkbox" name="Systemrole" value="ExtvideoCate_CreatePost" title="添加">
                            <input type="checkbox" name="Systemrole" value="ExtvideoCate_EditPost" title="编辑">
                            <input type="checkbox" name="Systemrole" value="ExtvideoCate_Delete" title="删除">

                        </div>
                    </div>
                </div>
            </fieldset>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input asp-for="Id" value="" class="layui-input" type="hidden">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn">@ViewData["Title"]</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'upload', 'jquery','tree'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery, tree = layui.tree;
        $.getJSON('/Admin/Category/GetTree', function (res) { //?role=1  时，带权限判断的栏目组
            tree.render({
                elem: '#tree',
                data: res,
                id: "Id",
                showCheckbox: true,
                //click: function (res) {
                //    // console.log("obj", res.data.id);
                //    // $("#main").load("/Admin/Content/Index?cid=" + res.data.id);} ,

                //oncheck: function (obj) {
                //      checkData = tree.getChecked('cid'); //获取选中节点的数据
                //     console.log("cid:",checkData)
                // }
            });
        })

        form.render();

        var editId = @ViewBag.id+1;
        if (editId > 1) {
            $.getJSON("/Admin/UserGroup/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                if (res.status == 1) {
                    form.val('FormEdit', {
                        "Id": res.data.id,
                        "Groupname": res.data.groupname,
                        //"Systemrole": res.data.systemrole,
                        //"Channlrole": res.data.channlrole,
                    });
                    //复选框选中
                    let data = JSON.parse(res.data.systemrole);
                    //console.log("data:", data);
                    $.each(data, (v) => {
                        let node = $(`input[type="checkbox"][name^="Systemrole"][value="${data[v]}"]`);
                        if (node && node.length) {
                            node[0].checked = true;
                            form.render('checkbox');
                        }
                    });
                    //栏目选中
                    tree.setChecked('Id', JSON.parse(res.data.channlrole));
                }
            });
        }

     var getCheckedId = function (jsonObj) {
         var id = '';
         $.each(jsonObj, function (index, item) {
             if (id != "") {
                 id = id + "," + item.id;
             }
             else {
                 id = item.id;
             }

                 var i = getCheckedId(item.children);
                 if (i != "") {
                     id = id + "," + i;
                 }
             
         });
         // var stringResult = id.split(',');
         return id;
     }

        var dataPost = function (url,data) {
            $.post(url, data, function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/UserGroup/Index/');
                    });
                } else {
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                }
            });
        }

        //监听提交
        $('#saveBtn').click(function () {
            let field = form.val('FormEdit');

            quotation = new Array();
            $("input:checkbox[name='Systemrole']:checked").each(function () {
                quotation.push($(this).val());
            });
            let SystemroleJson = JSON.stringify(quotation);

            let checkData = tree.getChecked('Id');
            // console.log("cid2:", checkData)

            let post = {
                Id: field.Id==null ? 0 : field.Id,
                Groupname: field.Groupname,
                Systemrole: SystemroleJson,
                Channlrole: JSON.stringify(getCheckedId(checkData).split(',')),
            };
            //console.log("checkData", JSON.stringify(getCheckedId(checkData).split(',')));

            if (editId == 1)
                dataPost('/Admin/UserGroup/CreatePost', post);
             else
                dataPost('/Admin/UserGroup/EditPost?id=@ViewBag.id', post);
            return false;
         });
    });

</script>